package com.csiec.clock.adpter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.csiec.clock.R;
import com.csiec.clock.activity.daka.ClockInfoActivity;
import com.csiec.clock.bean.MyClock;

import java.util.List;

public class MyClockAdapter extends RecyclerView.Adapter<MyClockAdapter.ViewHolder> {

    private final String TAG = MyClockAdapter.class.getSimpleName();

    private final List<MyClock> allClock;
    private final Activity activity;

    private final int[] bgList = {R.drawable.status_red, R.drawable.status_green, R.drawable.status_yellow};
    private final String[] textList = {"进行中", "已结束", "未开始"};

    public MyClockAdapter(List<MyClock> allClock, Activity activity) {
        this.allClock = allClock;
        this.activity = activity;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View itemView;
        itemView = layoutInflater.inflate(R.layout.cell_card, parent, false);
        return new ViewHolder(itemView);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MyClock clock = allClock.get(position);
        holder.clock_num.setText(String.valueOf(position + 1));
        holder.clock_name.setText(clock.getName());
        holder.clock_time.setText(clock.getSingleTime());
        holder.clock_state.setBackgroundResource(bgList[clock.getState()]);
        holder.clock_state.setText(textList[clock.getState()]);

        holder.constraintLayout.setOnClickListener(v -> {
            //跳转打卡详情页面
            MyClock cardSend = allClock.get(position);
            Intent intent = new Intent(activity.getApplication(), ClockInfoActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("card", cardSend);
            activity.getApplication().startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return allClock.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView clock_num, clock_name, clock_time, clock_state;
        ConstraintLayout constraintLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            clock_num = itemView.findViewById(R.id.clock_num);
            clock_name = itemView.findViewById(R.id.clock_name);
            clock_time = itemView.findViewById(R.id.clock_state);
            clock_state = itemView.findViewById(R.id.clock_time);
            constraintLayout = itemView.findViewById(R.id.join_card_item);
        }
    }

}
